﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cecbData;

namespace cecbClasses.Data_Adapters
{
    public class Func_FillMatrix
    {

        public Func_FillMatrix()
            {
                cecbContext = new DSSEntities();
            }

            DSSEntities cecbContext;
            LeopoldMatrixValues matrixVal;

         
            public bool InsertLeopoldValues(String impR, String actR, String proR, String impV, String magV)
            {
                bool IsInserted = false;
                try
                {
                    matrixVal = new LeopoldMatrixValues();

                    matrixVal.impt_reference = impR;
                    matrixVal.actv_reference = actR;  
                    matrixVal.proj_reference = proR;
                    matrixVal.mtrx_importance = double.Parse(impV);
                    matrixVal.mtrx_magnitude = double.Parse(magV);

                    cecbContext.AddToLeopoldMatrixValues(matrixVal);
                    cecbContext.SaveChanges();

                    IsInserted = true;
                }
                catch (Exception)
                {

                    IsInserted = false;
                     
                }


                return IsInserted;
            }


            public bool ConfirmDuplicatedValuesLeo(String impR, String actR, String proR, String impV, String magV, String memR)
            {
                bool IsInserted = false;
                try
                {
                    matrixVal = new LeopoldMatrixValues();

                    matrixVal.impt_reference = impR;
                    matrixVal.actv_reference = actR;
                    matrixVal.proj_reference = proR;
                     
                    matrixVal.mtrx_importance = double.Parse(impV);
                    matrixVal.mtrx_magnitude = double.Parse(magV);
                    matrixVal.mtrx_isRelated = true;

                    cecbContext.AddToLeopoldMatrixValues(matrixVal);
                    cecbContext.SaveChanges();

                    IQueryable<LeopoldMatrixValuesExtra> leox2 = from i in cecbContext.LeopoldMatrixValuesExtra
                                                                 where i.actv_reference == actR &&
                                                                                              i.impt_reference == impR &&
                                                                                              i.proj_reference == proR
                                                                 select i;

                    foreach (LeopoldMatrixValuesExtra x in leox2)
                    {
                        cecbContext.DeleteObject(x);
                    }
                    cecbContext.SaveChanges();

                    IsInserted = true;
                }
                catch (Exception)
                {

                    IsInserted = false;

                }


                return IsInserted;
            }

            public bool ConfirmDuplicatedValuesSimple(String impR, String actR, String proR, String impV , String memR)
            {
                bool IsInserted = false;
                try
                {
                    DSSEntities dataContext = new DSSEntities();
                    SimpleMatrixValues obj = new SimpleMatrixValues();
                    obj.actv_reference = actR;
                    obj.impt_reference = impR;
                    obj.proj_reference = proR;
                    obj.mtrx_value = int.Parse(impV);
                    dataContext.AddToSimpleMatrixValues(obj);
                    dataContext.SaveChanges();
                    
                     

                    IQueryable<SimpleMatrixValuesExtra> leox2 = from i in dataContext.SimpleMatrixValuesExtra
                                                                 where i.actv_reference == actR &&
                                                                                              i.impt_reference == impR &&
                                                                                              i.proj_reference == proR
                                                                 select i;

                    foreach (SimpleMatrixValuesExtra x in leox2)
                    {
                        dataContext.DeleteObject(x);
                    }
                    dataContext.SaveChanges();

                    IsInserted = true;
                }
                catch (Exception)
                {


                    IsInserted = false;

                }


                return IsInserted;
            }
            public bool UpdateValuesLeopold(String impR, String actR, String proR, String impV, String magV)
            {
                bool IsInserted = false;
                try
                {

                    LeopoldMatrixValues c = cecbContext.LeopoldMatrixValues.First(i => i.actv_reference == actR && i.impt_reference == impR && i.proj_reference == proR);               
                    c.mtrx_importance = double.Parse(impV);
                    c.mtrx_magnitude = double.Parse(magV);

                     cecbContext.SaveChanges();
                     IQueryable<LeopoldMatrixValuesExtra> leox2 = from i in cecbContext.LeopoldMatrixValuesExtra
                                                                  where i.actv_reference == actR &&
                                                                                               i.impt_reference == impR &&
                                                                                               i.proj_reference == proR
                                                                  select i;

                     foreach (LeopoldMatrixValuesExtra x in leox2)
                     {
                         cecbContext.DeleteObject(x);
                     }
                     cecbContext.SaveChanges();

                    IsInserted = true;
                }
                catch (Exception)
                {

                    IsInserted = false;
                }


                return IsInserted;
            }
            public bool UpdateValuesLeopoldOnly(String impR, String actR, String proR, String impV, String magV)
            {
                bool IsInserted = false;
                try
                {
                    DSSEntities cecbContext = new DSSEntities();
                    LeopoldMatrixValues c = cecbContext.LeopoldMatrixValues.First(i => i.actv_reference == actR && i.impt_reference == impR && i.proj_reference == proR);
                    c.mtrx_importance = double.Parse(impV);
                    c.mtrx_magnitude = double.Parse(magV);

                    cecbContext.SaveChanges();
                    

                    IsInserted = true;
                }
                catch (Exception)
                {

                    IsInserted = false;
                }


                return IsInserted;
            }
            public bool UpdateValuesSimple(String impR, String actR, String proR, String impV )
            {
                bool IsInserted = false;
                try
                {

                    SimpleMatrixValues c = cecbContext.SimpleMatrixValues.First(i => i.actv_reference == actR && i.impt_reference == impR && i.proj_reference == proR);
                    c.mtrx_value = int.Parse(impV);
                    
                    cecbContext.SaveChanges();


                    IQueryable<SimpleMatrixValuesExtra> leox2 = from i in cecbContext.SimpleMatrixValuesExtra
                                                                where i.actv_reference == actR &&
                                                                                             i.impt_reference == impR &&
                                                                                             i.proj_reference == proR
                                                                select i;

                    foreach (SimpleMatrixValuesExtra x in leox2)
                    {
                        cecbContext.DeleteObject(x);
                    }
                    cecbContext.SaveChanges();

                    IsInserted = true;
                }
                catch (Exception)
                {

                    IsInserted = false;
                }


                return IsInserted;
            }

    


       
    }
}
